# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libproxy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.4.15
pkgrel=4
pkgdesc="A library that provides automatic proxy configuration management (mingw-w64)"
arch=('any')
url="https://libproxy.github.io/libproxy/"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python3")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/libproxy/libproxy/archive/${pkgver}.tar.gz"
        001-fix-casting-error.patch
        002-fix-win32-close-scoket.patch
        004-allow-bindings.patch
        005-modules.patch
        006-python-site-package-dir.patch
        007-python37-support.patch
        008-fix-buffer-overflow.patch)
sha256sums=('18f58b0a0043b6881774187427ead158d310127fc46a1c668ad6d207fb28b4e0'
            'f20ce4f17c1a858b3eac84a14226c5e368ea581b0b5ab3428f6e1f9d22d44124'
            '88e5988413ee2f24a893c4a64f43a7574a395ae63139d05e66a8eb1d14f14902'
            '0c3182da531c117f80e05871af73d1c2c5d3828ee828f91bba212c9252747ddc'
            'c2e2193a6cafcd45dddc1993ea1e61a85c7d9e7b99a5f0e05a0754c52f5ac0dd'
            '035efcd364e736f8987ea99d11250b3e2987ba31cefbd434003faf0b3da789b6'
            '30146d1bedf41fc0010f7efc67348cf154b585493ee8e4fea76d7d95bda4e395'
            '5d3d9b7c321745d7daadd5e359d724906423d3e9b3087aeaa369af1e7b30491b')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-fix-casting-error.patch
  patch -p1 -i ${srcdir}/002-fix-win32-close-scoket.patch
  patch -p1 -i ${srcdir}/004-allow-bindings.patch
  patch -p1 -i ${srcdir}/005-modules.patch
  patch -p1 -i ${srcdir}/006-python-site-package-dir.patch
  patch -p1 -i ${srcdir}/007-python37-support.patch
  patch -p1 -i ${srcdir}/008-fix-buffer-overflow.patch # https://github.com/libproxy/libproxy/pull/126
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DLIBEXEC_INSTALL_DIR=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DLIBEXEC_INSTALL_DIR=${MINGW_PREFIX}/lib/libproxy \
    -DCMAKE_SKIP_RPATH=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS}" \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DWITH_PERL=OFF \
    -DWITH_PYTHON2=OFF \
    -DWITH_PYTHON3=ON \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
